import requests
import re


class Login:
    def __init__(self, username='253775405@qq.com', password='111111', result=None,
                 url='http://192.168.43.214/ty18/TinyShop_v1.7'):
        self.username = username
        self.password = password
        self.session = requests.session()
        self.result = result
        self.url = url

    def login(self):
        url = "%s/index.php?con=simple&act=login_act" % self.url
        data = {
            'redirectURL': '',
            'email': self.username,
            'password': self.password
        }
        self.session.post(url, data=data)
        url1 = '%s/index.php?con=ucenter&act=order' % self.url
        self.html = self.session.get(url1).text

    def compile(self):
        # passent = re.compile(r"{user:{name:'(.*)',id:'(.*)',online:true}}")
        # name = passent.findall(self.html)[0][0]
        if self.username in self.html:
            self.res = '登录成功'
        else:
            self.res = '登录失败'

    def start(self):
        try:
            self.login()
            self.compile()
        except Exception:
            self.res = '登录失败'
        return self.session, self.res


if __name__ == "__main__":
    Login().start()
